// Fill out your copyright notice in the Description page of Project Settings.


#include "Items/Soul.h"
#include "Interface/PickupInterface.h"
#include "Kismet/KismetSystemLibrary.h"

void ASoul::BeginPlay()
{
	Super::BeginPlay();
	const FVector Start = GetActorLocation();
	const FVector End = Start - FVector(0.f, 0.f, 2000.f);

	TArray<TEnumAsByte<EObjectTypeQuery>> ObjectType;
	ObjectType.Add(EObjectTypeQuery::ObjectTypeQuery1);

	TArray<AActor*>AcotrsToIgnore;
	AcotrsToIgnore.Add(GetOwner());

	FHitResult OutHit;

	UKismetSystemLibrary::LineTraceSingleForObjects(
		this,
		Start,
		End,
		ObjectType,
		false,
		AcotrsToIgnore,
		EDrawDebugTrace::None,
		OutHit,
		true
	);
	 DesiredZ = OutHit.ImpactPoint.Z + 50.f;
}

void ASoul::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	const double LoctionZ = GetActorLocation().Z;
	if (LoctionZ > DesiredZ)
	{
		const FVector DeltaLocation = FVector(0.f, 0.f, DriftRate * DeltaTime);
		AddActorWorldOffset(DeltaLocation);
	}
}

void ASoul::OnSphereOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	IPickupInterface* PickupInterface = Cast<IPickupInterface>(OtherActor);
	if (PickupInterface)
	{
		PickupInterface->AddSoul(this);
		SpawnPickupSystem();
		SpawnPickupSound();
		Destroy();
	}
}
